package cn.magic.org.service.impl;

import cn.magic.basic.service.impl.BaseServiceImpl;
import cn.magic.basic.utils.PageList;
import cn.magic.org.domain.Department;
import cn.magic.org.mapper.DepartmentMapper;
import cn.magic.org.query.DepartmentQuery;
import cn.magic.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PatchMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
* @Description: 部门业务层实现类
* @Author: Mr.pc
* @Version: 1.0
* @Date:  2022/6/21 18:03
*/
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;




    /**
    * @Title: update
    * @Description: 通过部门id修改数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/21 18:00
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        // 给dirPath拼接值
        String dirPath ="";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }

    /**
    * @Title: save
    * @Description: 新增数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/21 18:02
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        // 给dirPath拼接值
        String dirPath ="";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
    * @Title: tree
    * @Description: 获取部门树数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/25 21:42
    * @Parameters: []
    * @Return java.util.List<cn.magic.org.domain.Department>
    */
    @Override
    @Transactional
    public List<Department> tree() {
        // 一次性查询出全部数据
        List<Department> depts = departmentMapper.loadAll();
        // 创建一个集合对象，用于最终存放排列完成的树数据
        List<Department> deptTree = new ArrayList<Department>();
        // 用集合数据装换为map。消除时间复杂问题
        // Map<Long, Department> deptMap = deptTree.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            // 将排列完成的树数据赋值给新集合，通过key ，value
            deptMap.put(department.getId(),department);
        }
        // 在所有数据中找到一级路径
        for (Department deptTmp : depts) {
            if (deptTmp.getParentId()== null) {
                // 如果没有父级id，那么表示一级数据.将数据添加到新集合deptTree中
                deptTree.add(deptTmp);
            }else {
                // 从map集合中获取父类对象，将自己添加到父类对象的children集合中
                Department department = deptMap.get(deptTmp.getParentId());
                // 判断department是否为null
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }
        return deptTree;
    }


     /*@Override
    public List<Department> tree() {

        *//*
            -
            - 从所有数据中找到一级数据
            - 只要不是一级数据那就找到自己的上级数据，放入到上级数据的children中
        *//*
        // 1.一次性查询出全部数据
        List<Department> depts = departmentMapper.loadAll();

        // 2.创建一个集合对象，用于放最终排列完成的树数据
        List<Department> deptTree = new ArrayList<Department>();

        // 2.从所有数据中找到一级数据
        for (Department deptTmp : depts) {
            // 如果数据没有父级Id，那么就是一级数据
            if(deptTmp.getParent_id() == null){
                deptTree.add(deptTmp);
            }else {
                // 能进入到这里的数据就不是一级数据，那么我们要找到他的父亲
                for (Department parent : depts) {
                    // 判断外层循环的对象是否是内层循环对象的子数据，如果是放入到父对象的children集合中
                    if(parent.getId() == deptTmp.getParent_id()){
                        parent.getChildren().add(deptTmp);
                        // 当儿子找到父亲时，内层循环也就没有了意义，所以结束内层循环
                        break;
                    }
                }
            }
        }

        return deptTree;
    }*/
}
